var requests = require('request').defaults({
    forever: true
});

class RestHepler {
    constructor() {
    }

    static _onRequest(_uri, _action, _param) {
        let opts = {
            uri: _uri,
            json: true,
            rejectUnauthorized:false,
            method: _action
        };

        if (_param != null) {
            opts['body'] = _param;
        }
        return new Promise((resolve, reject) =>{
            requests(opts, (error, response, body) => {
            if (error) {
                reject(error);
                return;
            }
            resolve(body);
            });
        });
    }

    static get(_uri){
        return RestHepler._onRequest(_uri, "get", null);
    }
    static put(_uri, _param){
        return RestHepler._onRequest(_uri, "put", _param);
    }
    static post(_uri, _param){
        return RestHepler._onRequest(_uri, "post", _param);
    }
    static delete(_uri, _param, _func){
        return RestHepler._onRequest(_uri, "delete", _param);
    }
}

module.exports = RestHepler;
